﻿using FOModels.OverdraftLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccess.OverdraftLoan
{
    public class OverdraftLoanDAL
    {
        private SQLDataAccess db;
        public OverdraftLoanDAL()
        {
            db = new SQLDataAccess();
        }
        public DataTable getTOP1()
        {
            return db.exeSelect("Select TOP 1 * from OverdraftLoan order by OverdraftLoanID desc");
        }
        public DataTable getall(int langid)
        {
            return db.exeSelect("Select * from OverdraftLoan sa join OverdraftLoanLang hl on sa.OverdraftLoanID = hl.OverdraftLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join Language la on hl.LangID = la.ID  where hl.LangID = " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getOverdraftLoanrelati(int CompanyID, int OverdraftLoanID, int langid)
        {
            return db.exeSelect("select * from OverdraftLoan ca join OverdraftLoanLang hl on ca.OverdraftLoanID = hl.OverdraftLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.CompanyID = " + CompanyID + " and ca.OverdraftLoanID != " + OverdraftLoanID + " and Display = 1 and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getOverdraftLoan(int planid, string balance, int years, int langid)
        {
            return db.exeSelect("Select * from OverdraftLoan sa join OverdraftLoanLang hl on sa.OverdraftLoanID = hl.OverdraftLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableOverdraftLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getOverdraftLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            return db.exeSelect("Select * from OverdraftLoan sa join OverdraftLoanLang hl on sa.OverdraftLoanID = hl.OverdraftLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableOverdraftLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = " + planid + " and sa.MinimumBalance <= " + balance + " and sa.MaximumBalance >= " + balance + " and ta.Years = " + years + " and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID = " + CompanyID + "");
        }
        public DataTable getOverdraftLoanDefault(int langid)
        {
            return db.exeSelect("Select * from OverdraftLoan sa join OverdraftLoanLang hl on sa.OverdraftLoanID = hl.OverdraftLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableOverdraftLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + "");
        }
        public DataTable getOverdraftLoanDefaultByProvider(int CompanyID, int langid)
        {
            return db.exeSelect("Select * from OverdraftLoan sa join OverdraftLoanLang hl on sa.OverdraftLoanID = hl.OverdraftLoanID join Company co on sa.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID join TableOverdraftLoanInterest ta on co.CompanyID = ta.CompanyID where co.Active = 1 and sa.HomeLoanPlanID = 2 and sa.MinimumBalance <= 50000000 and sa.MaximumBalance >= 50000000 and ta.Years = 1 and Display = 1 and hl.LangID= " + langid + " and cl.LangID = " + langid + " and sa.CompanyID=" + CompanyID + "");
        }
        public DataTable getOverdraftLoanByID(int OverdraftLoanID, int langid)
        {
            return db.exeSelect("select * from OverdraftLoan ca join OverdraftLoanLang hl on ca.OverdraftLoanID = hl.OverdraftLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.OverdraftLoanID = " + OverdraftLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public DataTable getOverdraftLoanByIDManager(int OverdraftLoanID, int langid)
        {
            return db.exeSelect("select * from OverdraftLoan ca join OverdraftLoanLang hl on ca.OverdraftLoanID = hl.OverdraftLoanID join Company co on ca.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where ca.OverdraftLoanID = " + OverdraftLoanID + " and cl.LangID = " + langid + " and hl.LangID = " + langid + "");
        }
        public int UpdateOverdraftLoan(OverdraftLoanModels models)
        {
            return db.exeUpdate("update OverdraftLoan set OverdraftLoanName = N'" + models.OverdraftLoanName + "', MinimumBalance=" + double.Parse(models.MinimumBalance) + ",MaximumBalance=" + double.Parse(models.MaximumBalance) + ",HomeLoanPlanID=" + models.HomeLoanPlanID + ",CompanyID= " + models.CompanyID + ",Display='" + models.Display + "' where OverdraftLoanID = " + models.OverdraftLoanID + "");
        }
        public int InsertOverdraftLoan(OverdraftLoanModels models)
        {
            return db.exeUpdate("insert into OverdraftLoan values (N'" + models.OverdraftLoanName + "'," + models.CompanyID + "," + double.Parse(models.MinimumBalance) + "," + double.Parse(models.MaximumBalance) + "," + models.HomeLoanPlanID + ",'" + models.Display + "')");
        }
        public int DeleteOverdraftLoan(int OverdraftLoanID)
        {
            return db.exeUpdate("Delete from OverdraftLoan where OverdraftLoanID = " + OverdraftLoanID + "");
        }
        public int UpdateOverdraftLoanLang(OverdraftLoanModels models, int langid)
        {
            return db.exeUpdate("update OverdraftLoanLang set OverdraftLoanName = N'" + models.OverdraftLoanName + "', Benefit = N'" + models.Benefit + "',Description=N'" + models.Description + "',FeeContent=N'" + models.FeeContent + "',MoreInfo=N'" + models.MoreInfo + "' where OverdraftLoanID = " + models.OverdraftLoanID + " and LangID=" + langid + "");
        }
        public int InsertOverdraftLoanLang(OverdraftLoanModels models, int langid)
        {
            return db.exeUpdate("insert into OverdraftLoanLang values (" + models.OverdraftLoanID + "," + langid + ",N'" + models.OverdraftLoanName + "',N'" + models.Benefit + "',N'" + models.Description + "',N'" + models.FeeContent + "',N'" + models.MoreInfo + "')");
        }

        public int DeleteOverdraftLoanLang(int OverdraftLoanID)
        {
            return db.exeUpdate("Delete from OverdraftLoanLang where OverdraftLoanID = " + OverdraftLoanID + "");
        }

        public int DeleteOverdraftLoanApply(int OverdraftLoanApplyID)
        {
            return db.exeUpdate("Delete from OverdraftLoanApply where OverdraftLoanApplyID = " + OverdraftLoanApplyID + "");
        }
        public DataTable getcontractbyid(int OverdraftLoanapplyid)
        {
            return db.exeSelect("select * from OverdraftLoanApply ca join [OverdraftLoan] c on ca.OverdraftLoanID = c.OverdraftLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.OverdraftLoanApplyID = " + OverdraftLoanapplyid + " and cl.LangID = 1");
        }
        public DataTable getcontract()
        {
            return db.exeSelect("select * from OverdraftLoanApply ca join [OverdraftLoan] c on ca.OverdraftLoanID = c.OverdraftLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and cl.LangID = 1");
        }
        public DataTable getcontractbystatuscount(int statusid)
        {
            return db.exeSelect("select * from OverdraftLoanApply ca join [OverdraftLoan] c on ca.OverdraftLoanID = c.OverdraftLoanID join [Status] ta on ca.StatusID = ta.StatusID join Company co on c.CompanyID = co.CompanyID join CompanyLang cl on co.CompanyID = cl.CompanyID where co.Active = 1 and ca.StatusID = " + statusid + " and cl.LangID = 1");
        }
        public int OverdraftLoanApply(int OverdraftLoanID, OverdraftLoanApplyModels app)
        {
            return db.exeUpdate("insert into OverdraftLoanApply(OverdraftLoanID,CustomerName,CustomerEmail,CustomerPhone,CustomerIncome,CustomerDOB,StatusID) values (" + OverdraftLoanID + ",N'" + app.CustomerName + "',N'" + app.CustomerEmail + "',N'" + app.CustomerPhone + "',N'" + app.CustomerIncome + "',N'" + app.CustomerDOB + "'," + app.StatusID + ")");
        }
        public int UpdateStatus(OverdraftLoanApplyModels OverdraftLoan)
        {
            return db.exeUpdate("update OverdraftLoanApply set StatusID= " + OverdraftLoan.StatusID + " where OverdraftLoanApplyID = " + OverdraftLoan.OverdraftLoanApplyID + "");
        }
    }
}
